﻿'in order for a map to be randomly generated once entering "RandomMap" must be set to true, everything else is automatic after that, though for
'desired settings other variables for random generation may be adjusted.
Imports System.IO
Imports System.Xml
Module GenerateMap
#Region "Constants"
    Public Const North As Short = 0
    Const East As Short = 1
    Const South As Short = 2
    Const West As Short = 3

    Const Wall As Short = 0
    Const Floor As Short = 1
    Const StairsDown As Short = 3
    Const StairsUp As Short = 4

    Private Const MapWidth As Short = 29
    Private Const MapHeight As Short = 29
    Private Const MapSize As Short = 15
#End Region
#Region "Structures"
    Structure Mobile
        Dim Exists As Boolean
        Dim Name As String
        Dim Number As Short
        Dim Health As Integer
        Dim Willpower As Integer
        Dim Stamina As Integer
        Dim Xpos As Short
        Dim Ypos As Short
        Dim LowAttackDamage As Integer
        Dim HighAttackDamage As Integer
        Dim AttackSpeed
    End Structure
    Structure Directions
        Dim East(,) As Boolean
        Dim West(,) As Boolean
        Dim North(,) As Boolean
        Dim South(,) As Boolean
        Dim Up(,) As Boolean
        Dim Down(,) As Boolean
    End Structure
    Structure Map
        Dim Sector(,) As Short
        Dim Connections As Directions
        Dim Occupied(,) As Short
        'Map generation name
        Dim ProvinceType As String
        Dim EnvironmentType As String
        Dim MapType As String
        Dim SectorType As String
        Dim LocationTypeName As String
        Dim AddendumType As Boolean 'false=prefix, true=suffix
        Dim AddendumName As String
        'Map generation characteristics
        Dim AddendumDescription As String
        Dim MobSpawnMaximum As Byte
        Dim MobDifficulty As String
        Dim MobRespawnRate As Byte
        Dim ItemDropRate As Integer
        Dim GenerationType As String
    End Structure
#End Region
#Region "Dimensions Random Map"
    Public MapCreated As Boolean = False
    Public RandomMap As Boolean = False 'used to initiate whether the map is random or not
    Public CurrentMob(29) As Mobile 'support up to 30 mobiles per map though naturally not that many will spawn, it gives room for mobiles that will summon other mobiles.
    Public BattleMob As Short
    Private CurrentMap As New Map
    Private PlayerPosX As Short
    Private PlayerPosY As Short
#End Region
#Region "Functions Random Map"
    Public Function RemoveMob() As Boolean
        CurrentMob(BattleMob).Exists = False
        CurrentMap.Occupied(PlayerPosX, PlayerPosY) -= 1
        Return True
    End Function
    Public Function TestDirection(ByVal MoveDirection As String) As Boolean 'testdirection ensures that the direction will work, appropriates the necessary routines, then sends back to the command exa "southward" and removes the necessary stamina required to move
        If RandomMap = True Then
            If MoveDirection = "North" And PlayerPosY > 0 Then
                If CurrentMap.Sector(PlayerPosX, PlayerPosY - 1) <> Wall Then
                    Return True
                Else
                    Return False
                End If
            ElseIf MoveDirection = "South" And PlayerPosY < MapSize Then
                If CurrentMap.Sector(PlayerPosX, PlayerPosY + 1) <> Wall Then
                    Return True
                Else
                    Return False
                End If
            ElseIf MoveDirection = "East" And PlayerPosX < MapSize Then
                If CurrentMap.Sector(PlayerPosX + 1, PlayerPosY) <> Wall Then
                    Return True
                Else
                    Return False
                End If
            ElseIf MoveDirection = "West" And PlayerPosX > 0 Then
                If CurrentMap.Sector(PlayerPosX - 1, PlayerPosY) <> Wall Then
                    Return True
                Else
                    Return False
                End If
            ElseIf MoveDirection = "Up" Then
                If CurrentMap.Sector(PlayerPosX, PlayerPosY) = StairsUp Then
                    Return True
                Else
                    Return False
                End If
            ElseIf MoveDirection = "Down" Then
                If CurrentMap.Sector(PlayerPosX, PlayerPosY) = StairsDown Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function
    Private Function RetrieveGenerationTypes(ByVal LocationNumber As String, ByVal AddendumNumber As String) As Boolean
        'declare xml dimensions
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        Dim CurNumber As String
        Try
            m_xmld = New XmlDocument()
            m_xmld.Load(Replace(MainProgram.DirMapHeader, "MapHeader.TG", "GenerationTypes.xml"))
            m_nodelist = m_xmld.SelectNodes("/types/location/type")
            For Each m_node In m_nodelist
                CurNumber = m_node.Attributes.GetNamedItem("number").Value
                If CurNumber = LocationNumber Then 'location type requested was found
                    CurrentMap.MobSpawnMaximum = Val(m_node.Attributes.GetNamedItem("mobs").Value)
                    CurrentMap.MobRespawnRate = Val(m_node.Attributes.GetNamedItem("respawn").Value)
                    CurrentMap.GenerationType = m_node.Attributes.GetNamedItem("generation").Value
                    CurrentMap.LocationTypeName = m_node.InnerText
                    Exit For
                End If
            Next
            If CurrentMap.AddendumType = False Then
                m_nodelist = m_xmld.SelectNodes("/types/prefix/type")
            Else
                m_nodelist = m_xmld.SelectNodes("/types/suffix/type")
            End If
            For Each m_node In m_nodelist
                CurNumber = m_node.Attributes.GetNamedItem("number").Value
                If CurNumber = AddendumNumber Then 'prefix or suffix type requested was found
                    CurrentMap.MobDifficulty = m_node.Attributes.GetNamedItem("difficulty").Value
                    CurrentMap.ItemDropRate = Val(m_node.Attributes.GetNamedItem("items").Value)
                    CurrentMap.AddendumDescription = m_node.Attributes.GetNamedItem("description").Value
                    CurrentMap.AddendumName = m_node.InnerText
                    Exit For
                End If
            Next
            CurNumber = ""
        Catch errorVariable As Exception
            MainProgram.SND("/RERROR: " + errorVariable.ToString())
            Return False
        End Try
    End Function
    Private Function RetrieveEnvironment(ByVal number As String, ByVal province As String) As Boolean
        'declare xml dimensions
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        Dim CurNumber As String
        Try
            m_xmld = New XmlDocument()
            m_xmld.Load(Replace(MainProgram.DirMapHeader, "MapHeader.TG", "Environments.xml"))
            m_nodelist = m_xmld.SelectNodes("/environmentlist/environment")
            For Each m_node In m_nodelist
                CurNumber = m_node.Attributes.GetNamedItem("number").Value
                CurrentMap.EnvironmentType = m_node.Attributes.GetNamedItem("name").Value
                If CurNumber = number Then 'color requested was found
                    CurNumber = m_node.ChildNodes.Item(0).Attributes.GetNamedItem(province).Value
                    CurrentMap.SectorType = m_node.ChildNodes.Item(4).InnerText
                    If CurNumber = "1" Then
                        Return True
                    Else
                        Return False
                    End If
                End If
            Next
        Catch errorVariable As Exception
            MainProgram.SND("/RERROR: " + errorVariable.ToString())
            Return False
        End Try
    End Function
    Private Function RetrieveProvince(ByVal number As String) As Boolean
        'declare xml dimensions
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        Dim CurNumber As String
        Try
            m_xmld = New XmlDocument()
            m_xmld.Load(Replace(MainProgram.DirMapHeader, "MapHeader.TG", "Provinces.xml"))
            m_nodelist = m_xmld.SelectNodes("/provincelist/province")
            For Each m_node In m_nodelist
                CurNumber = m_node.Attributes.GetNamedItem("number").Value
                If CurNumber = number Then 'found the province
                    CurrentMap.ProvinceType = m_node.ChildNodes.Item(0).InnerText
                    Return True
                End If
            Next
        Catch errorVariable As Exception
            MainProgram.SND("/RERROR: " + errorVariable.ToString())
            Return False
        End Try
    End Function
#End Region
#Region "Random Map Routines"
    Public Sub BeginGeneration()
        ClearMap()
        GenerateMap(8)
        GenerateEntrances()
        PopulateMobiles()
        ShowRoom()
        MapCreated = True
    End Sub
    Sub Kill(ByVal name As String)
        If CurrentMap.Occupied(PlayerPosX, PlayerPosY) > 0 Then
            Dim CurMob As Short, found As Boolean
            For CurMob = 0 To 9 Step 1
                'make sure the mob is the correct name, the correct position, and living before attacking it
                If InStr(LCase(CurrentMob(CurMob).Name), LCase(name)) And CurrentMob(CurMob).Xpos = PlayerPosX And CurrentMob(CurMob).Ypos = PlayerPosY And CurrentMob(CurMob).Exists = True Then
                    found = True
                    Exit For
                End If
            Next
            If found = True Then
                BattleMob = CurMob 'this is set so that when the battle is over, if the mobile is killed then it is set as dead appropriately
                MainProgram.MobData_MNAME = LCase(CurrentMob(CurMob).Name)
                MainProgram.MobData_HITPO = CurrentMob(CurMob).Health
                MainProgram.MobData_WILLP = CurrentMob(CurMob).Willpower
                MainProgram.MobData_LEVEL = MainProgram.User_Level
                MainProgram.MobData_DamageName(0) = "Bite"
                MainProgram.MobData_DamageCrit(0) = "x2 Crit Value"
                MainProgram.MobData_DamageDieNum(0) = "1"
                MainProgram.MobData_DamageDieType(0) = "6"
                MainProgram.MobData_DamageSpeed(0) = "30"
                MainProgram.MobData_DamageType(0) = "Pierce"
                MainProgram.State = "Combat"
                MainProgram.StartCombat = True
            Else
                MainProgram.SND("/W'/w" + name + "/W' is not here.")
            End If
        Else
            MainProgram.SND("There is nobody here.")
        End If
    End Sub
    Sub PopulateMobiles()
        Dim RandomNum As New Random
        Dim RandomPosX As Short = RandomNum.Next(1, MapSize - 1) 'don't want to start a mobile on the edge of the map... just because it doesn't look pretty
        Dim RandomPosY As Short = RandomNum.Next(1, MapSize - 1) 'don't want to start a mobile on the edge of the map... just because it doesn't look pretty
        Dim MobileNumber As Short 'otherwise known in other parts as MobNum
        Dim FoundPosition As Boolean = False
        'initiate population
        Dim Tries As Short = 0
        For MobileNumber = 0 To 9 Step 1
            FoundPosition = False
            Tries = 0
            While FoundPosition = False
                Tries += 1
                If Tries > 100 Then
                    'no place to put the item, recursion too high, exit (catch)
                    Exit While
                End If
                RandomPosX = RandomNum.Next(1, MapSize - 1) 'don't want to start a mobile on the edge of the map... just because it doesn't look pretty
                RandomPosY = RandomNum.Next(1, MapSize - 1) 'don't want to start a mobile on the edge of the map... just because it doesn't look pretty
                If CurrentMap.Sector(RandomPosX, RandomPosY) = Floor Then
                    CurrentMap.Occupied(RandomPosX, RandomPosY) += 1  'assign mobiles random type
                    CurrentMob(MobileNumber).Name = "A rat"
                    CurrentMob(MobileNumber).Xpos = RandomPosX : CurrentMob(MobileNumber).Ypos = RandomPosY
                    CurrentMob(MobileNumber).Health = 10 + MainProgram.User_Level * 10
                    CurrentMob(MobileNumber).Exists = True 'set mobile to living
                    FoundPosition = True
                End If
            End While
        Next
    End Sub
    Public Sub MoveInDirection(ByVal MoveDirection As String)
        If MoveDirection = "North" And PlayerPosY > 0 Then
            MainProgram.SND("You move North.")
            PlayerPosY -= 1
            CoverExistingFloor(PlayerPosX, PlayerPosY)
            CoverExistingFloor(PlayerPosX, PlayerPosY + 1) 'cover existing spot
        ElseIf MoveDirection = "South" And PlayerPosY < MapSize Then
            MainProgram.SND("You move South.")
            PlayerPosY += 1
            CoverExistingFloor(PlayerPosX, PlayerPosY)
            CoverExistingFloor(PlayerPosX, PlayerPosY - 1) 'cover existing spot
        ElseIf MoveDirection = "East" And PlayerPosX < MapSize Then
            MainProgram.SND("You move East.")
            PlayerPosX += 1
            CoverExistingFloor(PlayerPosX, PlayerPosY)
            CoverExistingFloor(PlayerPosX - 1, PlayerPosY) 'cover existing spot
        ElseIf MoveDirection = "West" And PlayerPosX > 0 Then
            MainProgram.SND("You move West.")
            PlayerPosX -= 1
            CoverExistingFloor(PlayerPosX, PlayerPosY)
            CoverExistingFloor(PlayerPosX + 1, PlayerPosY) 'cover existing spot
        ElseIf MoveDirection = "Up" Then
            'currently moving up just generates a completely new random map
            MapCreated = False
            LoadSector.LoadSector("random", False)
        ElseIf MoveDirection = "Down" Then
            'currently moving down just generates a completely new random map
            MapCreated = False
            LoadSector.LoadSector("random", False)
        Else
            MainProgram.SND("/RERROR 382: Unspecified direction.")
            Exit Sub
        End If
        ShowRoom()
    End Sub
    Sub ShowRoom()
        Dim DrawString As String = "" : Dim SafeRoom As String = ""
        If CurrentMap.Connections.North(PlayerPosX, PlayerPosY) = True Then DrawString += "/RN/r" Else DrawString += "/rN"
        If CurrentMap.Connections.South(PlayerPosX, PlayerPosY) = True Then DrawString += "/RS/r" Else DrawString += "/rS"
        If CurrentMap.Connections.East(PlayerPosX, PlayerPosY) = True Then DrawString += "/RE/r" Else DrawString += "/rE"
        If CurrentMap.Connections.West(PlayerPosX, PlayerPosY) = True Then DrawString += "/RW/r" Else DrawString += "/rW"
        If CurrentMap.Connections.Up(PlayerPosX, PlayerPosY) = True Then DrawString += "/RU/r" Else DrawString += "/rU"
        If CurrentMap.Connections.Down(PlayerPosX, PlayerPosY) = True Then DrawString += "/RD/r" Else DrawString += "/rD"
        If MainProgram.MapData_SafeRoom = "1" Then SafeRoom = "/WSafe Room"
        MainProgram.SND("/zExits: /r[" + DrawString + "] " + SafeRoom)
        MainProgram.SND("/w" + CurrentMap.SectorType)
        If PlayerPosY > 0 Then
            If CurrentMap.Sector(PlayerPosX, PlayerPosY - 1) <> Wall Then
                MainProgram.MainFlash.TGotoFrame("North", 0)
                'thenextline indicates the amount of people that are in the next room, not currently available
                MainProgram.MainFlash.SetVariable("North.Northnum", LTrim(Str(CurrentMap.Occupied(PlayerPosX, PlayerPosY - 1))))
            Else
                MainProgram.MainFlash.TGotoFrame("North", 2)
            End If
        End If
        If PlayerPosX < MapSize Then
            If CurrentMap.Sector(PlayerPosX + 1, PlayerPosY) <> Wall Then
                MainProgram.MainFlash.TGotoFrame("East", 0)
                'thenextline indicates the amount of people that are in the next room, not currently available
                MainProgram.MainFlash.SetVariable("East.Eastnum", LTrim(Str(CurrentMap.Occupied(PlayerPosX + 1, PlayerPosY))))
            Else
                MainProgram.MainFlash.TGotoFrame("East", 2)
            End If
        End If
        If PlayerPosX > 0 Then
            If CurrentMap.Sector(PlayerPosX - 1, PlayerPosY) <> Wall Then
                MainProgram.MainFlash.TGotoFrame("West", 0)
                'thenextline indicates the amount of people that are in the next room, not currently available
                MainProgram.MainFlash.SetVariable("West.Westnum", LTrim(Str(CurrentMap.Occupied(PlayerPosX - 1, PlayerPosY))))
            Else
                MainProgram.MainFlash.TGotoFrame("West", 2)
            End If
        End If
        If CurrentMap.Sector(PlayerPosX, PlayerPosY) = StairsDown Then
            MainProgram.MainFlash.TGotoFrame("Down", 0)
            'thenextline indicates the amount of people that are in the next room, not currently available
            MainProgram.MainFlash.SetVariable("Down.Downnum", LTrim(Str(0)))
        Else
            MainProgram.MainFlash.TGotoFrame("Down", 2)
        End If
        If CurrentMap.Sector(PlayerPosX, PlayerPosY) = StairsUp Then
            MainProgram.MainFlash.TGotoFrame("Up", 0)
            'thenextline indicates the amount of people that are in the next room, not currently available
            MainProgram.MainFlash.SetVariable("Up.Upnum", LTrim(Str(0)))
        Else
            MainProgram.MainFlash.TGotoFrame("Up", 2)
        End If
        If PlayerPosY < MapSize Then
            If CurrentMap.Sector(PlayerPosX, PlayerPosY + 1) <> Wall Then
                MainProgram.MainFlash.TGotoFrame("South", 0)
                'thenextline indicates the amount of people that are in the next room, not currently available
                MainProgram.MainFlash.SetVariable("South.Southnum", LTrim(Str(CurrentMap.Occupied(PlayerPosX, PlayerPosY + 1))))
            Else
                MainProgram.MainFlash.TGotoFrame("South", 2)
            End If
        End If
        'thenextline indicates the amount of people that are in the next room, not currently available
        MainProgram.MainFlash.SetVariable("Cur.Curnum", LTrim(Str(CurrentMap.Occupied(PlayerPosX, PlayerPosY) + 1)))
        If CurrentMap.Occupied(PlayerPosX, PlayerPosY) <> 0 Then
            Dim CurMob As Short = 0
            For CurMob = 0 To 9 Step 1
                'make sure the mob has a name, is at the correct position, and is alive before showing
                If CurrentMob(CurMob).Name <> "" And CurrentMob(CurMob).Xpos = PlayerPosX And CurrentMob(CurMob).Ypos = PlayerPosY And CurrentMob(CurMob).Exists = True Then
                    MainProgram.SND(CurrentMob(CurMob).Name + " is here.")
                End If
            Next
        End If
    End Sub
    Public Sub ClearMap()
        CurrentMap.Sector = New Short(MapWidth, MapHeight) {}
        CurrentMap.Occupied = New Short(MapWidth, MapHeight) {}
        CurrentMap.Connections.East = New Boolean(MapWidth, MapHeight) {}
        CurrentMap.Connections.West = New Boolean(MapWidth, MapHeight) {}
        CurrentMap.Connections.North = New Boolean(MapWidth, MapHeight) {}
        CurrentMap.Connections.South = New Boolean(MapWidth, MapHeight) {}
        CurrentMap.Connections.Up = New Boolean(MapWidth, MapHeight) {}
        CurrentMap.Connections.Down = New Boolean(MapWidth, MapHeight) {}
    End Sub
    Public Sub CoverExistingFloor(ByVal tmpx As Short, ByVal tmpy As Short)
        If tmpx = PlayerPosX And tmpy = PlayerPosY Then
            SetEnvironmentColor(tmpx, tmpy, "Player") 'indicates playerposition
        ElseIf CurrentMap.Sector(tmpx, tmpy) = Wall Then
            SetEnvironmentColor(tmpx, tmpy, "Nothing") 'this is emptiness
        ElseIf CurrentMap.Sector(tmpx, tmpy) = Floor Then
            SetEnvironmentColor(tmpx, tmpy, CurrentMap.EnvironmentType) 'this is the floor or spaces that exist
        ElseIf CurrentMap.Sector(tmpx, tmpy) = StairsDown Then
            SetEnvironmentColor(tmpx, tmpy, "Anchihaline Cave") 'this is stairs down
        ElseIf CurrentMap.Sector(tmpx, tmpy) = StairsUp Then
            SetEnvironmentColor(tmpx, tmpy, "Anchihaline Cave") 'this is stairs up
        Else
            SetEnvironmentColor(tmpx, tmpy, "888") 'indicates an error cell
        End If
    End Sub
    Public Sub GenerateEntrances()
        Dim RandomNum As New Random
        Dim RandomPosX As Short = RandomNum.Next(1, MapSize - 1)
        Dim RandomPosY As Short = RandomNum.Next(1, MapSize - 1)
        Dim EntrancePosX, EntrancePosY As Short
        Dim Foundentrance, Foundexit As Boolean
        Dim maplevel As Short
        While Foundentrance = False
            If CurrentMap.Sector(RandomPosX, RandomPosY) = 1 Then
                Foundentrance = True
                If maplevel >= 2 Then 'ensures that the player can't go to levels before 1
                    'Map(RandomPosX, RandomPosY) = 2 'uncomment this to allow stairs up
                    'EntrancePosX = RandomPosX 'uncomment this to allow stairs up
                    'EntrancePosY = RandomPosY 'uncomment this to allow stairs up
                End If
                PlayerPosX = RandomPosX
                PlayerPosY = RandomPosY
                MainProgram.MainFlash.SetVariable("colorprof.x" + LTrim(Str(PlayerPosX)) + "y" + LTrim(Str(PlayerPosY)) + "c", "0xFFFFFF")
                RandomPosX = RandomNum.Next(1, MapSize - 1) 'not necessary if stairs up is allowed, prevents stairs down from spawning on player
                RandomPosY = RandomNum.Next(1, MapSize - 1) 'not necessary if stairs up is allowed, prevents stairs down from spawning on player
            Else
                RandomPosX = RandomNum.Next(1, MapSize - 1)
                RandomPosY = RandomNum.Next(1, MapSize - 1)
            End If
        End While
        While Foundexit = False
            If CurrentMap.Sector(RandomPosX, RandomPosY) = 1 Then
                If Math.Abs(RandomPosX - EntrancePosX) >= 5 Or Math.Abs(RandomPosY - EntrancePosY) >= 5 Then
                    CurrentMap.Sector(RandomPosX, RandomPosY) = 3
                    Foundexit = True
                Else
                    RandomPosX = RandomNum.Next(1, MapSize - 1)
                    RandomPosY = RandomNum.Next(1, MapSize - 1)
                End If
            Else
                RandomPosX = RandomNum.Next(1, MapSize - 1)
                RandomPosY = RandomNum.Next(1, MapSize - 1)
            End If
        End While
        Showrandommap()
    End Sub
    Public Sub Showrandommap()
        Dim tmpx, tmpy As Short
        GenerateNames() 'this will generate the environment as well as the names and display them. This has to go at the top because the environment is used for coloring the cells below
        For tmpx = 0 To MapSize Step 1 'start with x's go from 0 to max mapsize
            For tmpy = 0 To MapSize Step 1 'go from 0 to max mapsize
                CoverExistingFloor(tmpx, tmpy)
                'now test to see if it connects to other cells, show that connection, and show it on the map
                If CurrentMap.Sector(tmpx, tmpy) <> Wall Then
                    If tmpx > 0 Then
                        If CurrentMap.Sector(tmpx - 1, tmpy) <> Wall Then
                            CurrentMap.Connections.West(tmpx, tmpy) = True
                        Else
                            CurrentMap.Connections.West(tmpx, tmpy) = False
                        End If
                    Else
                        CurrentMap.Connections.West(tmpx, tmpy) = False
                    End If
                    If tmpx < MapSize Then
                        If CurrentMap.Sector(tmpx + 1, tmpy) <> Wall Then
                            CurrentMap.Connections.East(tmpx, tmpy) = True
                        Else
                            CurrentMap.Connections.East(tmpx, tmpy) = False
                        End If
                    Else
                        CurrentMap.Connections.East(tmpx, tmpy) = False
                    End If
                    If tmpy > 0 Then
                        If CurrentMap.Sector(tmpx, tmpy - 1) <> Wall Then
                            CurrentMap.Connections.North(tmpx, tmpy) = True
                        Else
                            CurrentMap.Connections.North(tmpx, tmpy) = False
                        End If
                    Else
                        CurrentMap.Connections.North(tmpx, tmpy) = False
                    End If
                    If tmpy < MapSize Then
                        If CurrentMap.Sector(tmpx, tmpy + 1) <> Wall Then
                            CurrentMap.Connections.South(tmpx, tmpy) = True
                        Else
                            CurrentMap.Connections.South(tmpx, tmpy) = False
                        End If
                    Else
                        CurrentMap.Connections.South(tmpx, tmpy) = False
                    End If
                    If CurrentMap.Sector(tmpx, tmpy) = StairsDown Then
                        CurrentMap.Connections.Down(tmpx, tmpy) = True
                    Else
                        CurrentMap.Connections.Down(tmpx, tmpy) = False
                    End If
                    If CurrentMap.Sector(tmpx, tmpy) = StairsUp Then
                        CurrentMap.Connections.Up(tmpx, tmpy) = True
                    Else
                        CurrentMap.Connections.Up(tmpx, tmpy) = False
                    End If
                Else
                    CurrentMap.Connections.North(tmpx, tmpy) = False
                    CurrentMap.Connections.South(tmpx, tmpy) = False
                    CurrentMap.Connections.East(tmpx, tmpy) = False
                    CurrentMap.Connections.West(tmpx, tmpy) = False
                    CurrentMap.Connections.Up(tmpx, tmpy) = False
                    CurrentMap.Connections.Down(tmpx, tmpy) = False
                End If
                If tmpx > 0 And tmpy > 0 Then
                    If CurrentMap.Connections.North(tmpx, tmpy) = True Then
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "n", 0, LTrim(Str(-20 + 16.2 * tmpx)))
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "n", 1, LTrim(Str(-18 + 16.515 * tmpy)))
                    Else
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "n", 0, "-500")
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "n", 1, "-500")
                    End If
                    If CurrentMap.Connections.South(tmpx, tmpy) = True Then
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "s", 0, LTrim(Str(-20 + 16.2 * tmpx)))
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "s", 1, LTrim(Str(-10 + 16.515 * tmpy)))
                    Else
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "s", 0, "-500")
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "s", 1, "-500")
                    End If
                    If CurrentMap.Connections.East(tmpx, tmpy) = True Then
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "e", 0, LTrim(Str(-12 + 16.2 * tmpx)))
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "e", 1, LTrim(Str(-18 + 16.515 * tmpy)))
                    Else
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "e", 0, "-500")
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "e", 1, "-500")
                    End If
                    If CurrentMap.Connections.West(tmpx, tmpy) = True Then
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "w", 0, LTrim(Str(-20 + 16.2 * tmpx)))
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "w", 1, LTrim(Str(-18 + 16.515 * tmpy)))
                    Else
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "w", 0, "-500")
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "w", 1, "-500")
                    End If
                    If CurrentMap.Connections.Up(tmpx, tmpy) = True Then
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "u", 0, LTrim(Str(-20 + 16.2 * tmpx)))
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "u", 1, LTrim(Str(-18 + 16.515 * tmpy)))
                    Else
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "u", 0, "-500")
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "u", 1, "-500")
                    End If
                    If CurrentMap.Connections.Down(tmpx, tmpy) = True Then
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "d", 0, LTrim(Str(-20 + 16.2 * tmpx)))
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "d", 1, LTrim(Str(-18 + 16.515 * tmpy)))
                    Else
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "d", 0, "-500")
                        MainProgram.MainFlash.TSetProperty("connections.x" + LTrim(Str(tmpx)) + "y" + LTrim(Str(tmpy)) + "d", 1, "-500")
                    End If
                End If
            Next
        Next
    End Sub
    Public Sub GenerateMap(ByVal Recursion As Short)
        Dim RepeatToRecursion As Short = 0
        Dim RandomNumber As New Random
        Dim BuilderDirection As Short = 0
        Dim BuilderLastDirection As Short = 0
        Dim BuilderGrowthAmount As Short 'this is the actual amount that will be grown, not the potential
        Dim BuilderPositionX As Short = RandomNumber.Next(2, MapSize)
        Dim BuilderPositionY As Short = RandomNumber.Next(2, MapSize)
        Dim Turns As Short = 7
        Dim Stops = 0
        Dim StopWhile = 0
        Dim PotentialGrowth As Short = 0 'this number is the potential size the room or corridor can be in that direction
        Dim PotentialSides As Short = 0
        For RepeatToRecursion = 1 To Recursion Step 1
            CurrentMap.Sector(BuilderPositionX, BuilderPositionY) = 1
            While Turns > 0
                PotentialGrowth = 0
                '-------Pick Random Direction---------
                BuilderDirection = RandomNumber.Next(1, 5) 'There are 4 directions
                While BuilderDirection = BuilderLastDirection
                    BuilderDirection = RandomNumber.Next(1, 5) 'Sorry can't pick the last direction you grew
                    StopWhile += 1
                    If StopWhile = 10 Then
                        Stops += 1
                        If Stops > 100 Then Exit While
                        Exit While
                    End If
                End While
                StopWhile = 0
                If BuilderDirection = North And BuilderPositionY > 2 Then 'Verifying there is room for growth in that direction, at least 2 spaces
                    PotentialGrowth = BuilderPositionY - 1
                    If CurrentMap.Sector(BuilderPositionX, BuilderPositionY - 1) = 1 Then 'already carved, no need to proceed
                        PotentialGrowth = -1 : Stops += 1
                    End If
                ElseIf BuilderDirection = East And BuilderPositionX < MapSize - 2 Then 'Veryifying there is room for growth in that direction, at least 2 spaces
                    PotentialGrowth = MapSize - BuilderPositionX - 1
                    If CurrentMap.Sector(BuilderPositionX + 1, BuilderPositionY) = 1 Then 'already carved, no need to proceed
                        PotentialGrowth = -1 : Stops += 1
                    End If
                ElseIf BuilderDirection = South And BuilderPositionY < MapSize - 2 Then 'Verifying there is room for growth in that direction, at least 2 spaces
                    PotentialGrowth = MapSize - BuilderPositionY - 1
                    If CurrentMap.Sector(BuilderPositionX, BuilderPositionY + 1) = 1 Then 'already carved, no need to proceed
                        PotentialGrowth = -1 : Stops += 1
                    End If
                ElseIf BuilderDirection = West And BuilderPositionX > 2 Then 'Verifying there is room for growth in that direction, at least 2 spaces
                    PotentialGrowth = BuilderPositionX - 1
                    If CurrentMap.Sector(BuilderPositionX - 1, BuilderPositionY) = 1 Then 'already carved, no need to proceed
                        PotentialGrowth = -1 : Stops += 1
                    End If
                End If
                If PotentialGrowth > 4 Then
                    BuilderLastDirection = BuilderDirection
                    BuilderGrowthAmount = RandomNumber.Next(1, PotentialGrowth) 'growth includes wall, can't draw to end of map so just use potential growth since it's exclusive instead of inclusive
                    CurrentMap.Sector(BuilderPositionX + 1, BuilderPositionY) = 1
                    CurrentMap.Sector(BuilderPositionX - 1, BuilderPositionY) = 1
                    CurrentMap.Sector(BuilderPositionX, BuilderPositionY + 1) = 1
                    CurrentMap.Sector(BuilderPositionX, BuilderPositionY - 1) = 1
                    PotentialSides = RandomNumber.Next(1, 3)
                    If PotentialSides = 1 Then
                        CurrentMap.Sector(BuilderPositionX + 1, BuilderPositionY + 1) = 1
                    End If
                    PotentialSides = RandomNumber.Next(1, 3)
                    If PotentialSides = 1 Then
                        CurrentMap.Sector(BuilderPositionX - 1, BuilderPositionY - 1) = 1
                    End If
                    PotentialSides = RandomNumber.Next(1, 3)
                    If PotentialSides = 1 Then
                        CurrentMap.Sector(BuilderPositionX - 1, BuilderPositionY + 1) = 1
                    End If
                    PotentialSides = RandomNumber.Next(1, 3)
                    If PotentialSides = 1 Then
                        CurrentMap.Sector(BuilderPositionX + 1, BuilderPositionY - 1) = 1
                    End If
                    If BuilderDirection = North Then
                        For BuilderPositionY = BuilderPositionY To BuilderPositionY - BuilderGrowthAmount Step -1
                            CurrentMap.Sector(BuilderPositionX, BuilderPositionY) = 1
                            PotentialSides = RandomNumber.Next(1, 11)
                            If PotentialSides = 1 Then 'random chance to draw a floor to the right 10%
                                CurrentMap.Sector(BuilderPositionX + 1, BuilderPositionY) = 1
                            ElseIf PotentialSides = 2 Then 'randomchance to draw a floor to the left 10%
                                CurrentMap.Sector(BuilderPositionX - 1, BuilderPositionY) = 1
                            End If
                        Next
                    ElseIf BuilderDirection = East Then
                        For BuilderPositionX = BuilderPositionX To BuilderPositionX + BuilderGrowthAmount Step 1
                            CurrentMap.Sector(BuilderPositionX, BuilderPositionY) = 1
                            PotentialSides = RandomNumber.Next(1, 11)
                            If PotentialSides = 1 Then 'random chance to draw a floor to the north 10%
                                CurrentMap.Sector(BuilderPositionX, BuilderPositionY - 1) = 1
                            ElseIf PotentialSides = 2 Then 'randomchance to draw a floor to the south 10%
                                CurrentMap.Sector(BuilderPositionX, BuilderPositionY + 1) = 1
                            End If
                        Next
                    ElseIf BuilderDirection = South Then
                        For BuilderPositionY = BuilderPositionY To BuilderPositionY + BuilderGrowthAmount Step 1
                            CurrentMap.Sector(BuilderPositionX, BuilderPositionY) = 1
                            PotentialSides = RandomNumber.Next(1, 11)
                            If PotentialSides = 1 Then 'random chance to draw a floor to the right 10%
                                CurrentMap.Sector(BuilderPositionX + 1, BuilderPositionY) = 1
                            ElseIf PotentialSides = 2 Then 'randomchance to draw a floor to the left 10%
                                CurrentMap.Sector(BuilderPositionX - 1, BuilderPositionY) = 1
                            End If
                        Next
                    ElseIf BuilderDirection = West Then
                        For BuilderPositionX = BuilderPositionX To BuilderPositionX - BuilderGrowthAmount Step -1
                            CurrentMap.Sector(BuilderPositionX, BuilderPositionY) = 1
                            PotentialSides = RandomNumber.Next(1, 11)
                            If PotentialSides = 1 Then 'random chance to draw a floor to the north 10%
                                CurrentMap.Sector(BuilderPositionX, BuilderPositionY - 1) = 1
                            ElseIf PotentialSides = 2 Then 'randomchance to draw a floor to the south 10%
                                CurrentMap.Sector(BuilderPositionX, BuilderPositionY + 1) = 1
                            End If
                        Next
                    End If
                    Turns -= 1
                Else
                    Stops += 1
                    If Stops > 100 Then
                        Turns = 0
                    End If
                End If
                If Stops > 100 Then
                    Turns = 0
                End If
            End While
            Stops = 0
            StopWhile = 0
            BuilderDirection = 0
            BuilderLastDirection = 0
            Turns = 7
        Next
    End Sub
#End Region
#Region "Generate Names"
    Sub GenerateNames()
        'variable used for all random generation techniques in subroutine
        Dim RandomVar As New Random
        'variables used for province and area generation techniques
        Dim ProvinceRoll As Short
        Dim T, N, C, S, J, W As SByte
        Dim EnvironmentRoll As Integer
        'First of all roll for province which will limit the availability of environments
        'that can be chosen. Once a province is distinguished the environments are rolled
        'which are cross-examined and re-ran until they are found suitable.
        ProvinceRoll = RandomVar.Next(1, 7) '1-6
        If RetrieveProvince(LTrim(Str(ProvinceRoll))) = False Then
            Exit Sub
        End If
        'Naturally you can't go above a hundred, therefor when over a hundred is reached
        'it's because it was manually set to exit the while statement. Basically loop until
        'an environment is found that fits the province that was chosen.
        While EnvironmentRoll < 100
            EnvironmentRoll = RandomVar.Next(1, 100) '1-99
            If RetrieveEnvironment(LTrim(Str(EnvironmentRoll)), LCase(CurrentMap.ProvinceType)) = True Then
                EnvironmentRoll = 100
            End If
        End While
        CurrentMap.AddendumType = RandomVar.Next(0, 2) '0=false=Prefix,1=true=Suffix
        If CurrentMap.AddendumType = False Then 'prefix
            RetrieveGenerationTypes(LTrim(Str(RandomVar.Next(1, 52))), LTrim(Str(RandomVar.Next(1, 52))))
        Else 'suffix roll
            RetrieveGenerationTypes(LTrim(Str(RandomVar.Next(1, 52))), LTrim(Str(RandomVar.Next(1, 51))))
        End If
        'now if the prefix is blank (suffix won roll) then show the suffix, else show the prefix instead
        If CurrentMap.AddendumType = True Then 'suffix
            CurrentMap.LocationTypeName += " of " + CurrentMap.AddendumName
        Else 'prefix
            CurrentMap.LocationTypeName = CurrentMap.AddendumName + " " + CurrentMap.LocationTypeName
        End If
        'now assign all variables to map
        CurrentMap.MapType = CurrentMap.LocationTypeName
        MainProgram.UserLocation_ProvinceString = CurrentMap.ProvinceType
        MainProgram.MainFlash.SetVariable("sector.txta", MainProgram.SepByCapital(MainProgram.UserLocation_ProvinceString, " "))
        MainProgram.UserLocation_AreaString = CurrentMap.EnvironmentType 'environment poses as area in random maps
        MainProgram.MainFlash.SetVariable("sector.txtb", MainProgram.SepByCapital(MainProgram.UserLocation_AreaString, " "))
        MainProgram.UserLocation_MapString = CurrentMap.LocationTypeName 'yay for randomly generated location names, it will use either a prefix or suffix when displayed but never both
        MainProgram.MainFlash.SetVariable("sector.txtc", MainProgram.SepByCapital(MainProgram.UserLocation_MapString))
        MainProgram.UserLocation_SectorString = "Path" 'currently a path is all that's available for the current sector
        MainProgram.MainFlash.SetVariable("sector.txtd", MainProgram.SepByCapital(MainProgram.UserLocation_SectorString))
    End Sub
#End Region
End Module
